﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Sunny.UI;

namespace MMS
{
    public partial class Page_UpdataMember : UIPage
    {
        public Page_UpdataMember()
        {
            InitializeComponent();
        }
        public bool root { get; set; }
        db_mmsEntities conn = new db_mmsEntities();
        tb_member mem = new tb_member();
        private void Page_UpdateMember_Load(object sender, EventArgs e)
        {
            inIt();
            
            uiDataGridView5.SetRowHeight(30);

            btn_Reset.Enabled = false;
            if (!root)
            {
                lab_1.Visible = false;
                lab_2.Visible = false;
                lab_IDNb.Visible = false;
                txt_IDNb.Visible = false;
                btn_Add.Visible = false;
                com_MemState.Visible = false;
                com_MemType.Visible = false;
                txt_ReAddress.Enabled = false;
                txt_RePhone.Enabled = false;
            }
        }
        private void btn_Select_Click(object sender, EventArgs e)
        {
            uiDataGridView5.Rows.Clear();
            string id = txt_MemNb.Text;
            string mename = txt_memNm.Text;
            if ((id == "" || mename == ""))
            {
                MessageBox.Show("请填写每一项信息！");
                return;
            }
            mem = conn.tb_member.ToList().FirstOrDefault(r => r.memberID == id && r.memberName == mename);
            if (mem == null)
            {
                MessageBox.Show("该会员不存在");
                return;
            }
            string mtype;
            switch (mem.type)
            {
                case 0:
                    mtype = "白金卡";
                    break;
                case 1:
                    mtype = "金卡";
                    break;
                case 2:
                    mtype = "银卡";
                    break;
                default:
                    mtype = "";
                    break;
            }
            if (mem != null)
            {
                string[] temp = new string[] { mem.memberID, mem.memberName, mem.phone, mem.address, mtype , mem.isUse == 1 ? "可用" : "未激活" };
                uiDataGridView5.Rows.Add(temp);
            }
            else
            {
                MessageBox.Show("未查询到会员信息！");
            }

            
            
            txt_MemNb.Clear();
            txt_memNm.Clear();
            txt_ReAddress.Clear();
            txt_RePhone.Clear();

            btn_Reset.Enabled = true;
            txt_ReAddress.Enabled = true;
            txt_RePhone.Enabled = true;
        }
        /// <summary>
        /// 修改会员信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_Reset_Click(object sender, EventArgs e)
        {
            string rephone = txt_RePhone.Text;
            string address = txt_ReAddress.Text;
            


            if (rephone == "" || address == "")
            {
                MessageBox.Show("新信息填写有空！");
                return;
            }
            mem.phone = rephone;
            mem.address = address;

            //管理员权限
            if (root)
            {
                int state;
                int mtype;

                switch (com_MemState.Text)
                {
                    case "可用":
                        state = 1;
                        break;
                    case "不可用":
                        state = 0;
                        break;
                    default:
                        MessageBox.Show("请正确选择状态类型");
                        return;
                }
                switch (com_MemType.Text)
                {
                    case "白金卡":
                        mtype = 0;
                        break;
                    case "金卡":
                        mtype = 1;
                        break;
                    case "银卡":
                        mtype = 2;
                        break;
                    default:
                        MessageBox.Show("请正确选择会员类型");
                        return;

                }
                mem.type = mtype;
                mem.isUse = state;
            }


            conn.SaveChanges();

            uiDataGridView5.Rows.Clear();
            string[] temp = new string[] { mem.memberID, mem.memberName, mem.phone, mem.address, mem.isUse == 1 ? "可用" : "未激活" };
            uiDataGridView5.Rows.Add(temp);
            MessageBox.Show("修改成功！");
            btn_Reset.Enabled = false;
            if (!root)
            {
                txt_ReAddress.Enabled = false;
                txt_RePhone.Enabled = false;
            }
            
            
        }

        private void inIt()
        {
            if (root)
            {
                List<int> metype = conn.tb_rule.ToList().GroupBy(r => r.memberType).Select(k => Convert.ToInt32(k.Key)).ToList();
                List<string> ty = new List<string>();
                foreach (int i in metype)
                {
                    Console.WriteLine(i);
                    string t = "";
                    if (i == -1)
                    {
                        continue;
                    }
                    switch (i)
                    {
                        case 0://白金卡
                            t = "白金卡";
                            break;
                        case 1://金卡
                            t = "金卡";
                            break;
                        case 2://银卡
                            t = "银卡";
                            break;
                    }
                    ty.Add(t);
                }
                com_MemType.DataSource = ty;


            }
        }

        private void btn_Add_Click(object sender, EventArgs e)
        {
            string id = txt_MemNb.Text;
            //查询是否存在
            var temp = conn.tb_member.ToList().FirstOrDefault(r => r.memberID == id);
            if (temp != null)
            {
                MessageBox.Show("该会员已经存在！");
                return;
            }
            string mename = txt_memNm.Text;
            string rephone = txt_RePhone.Text;
            string address = txt_ReAddress.Text;
            string idnumber = txt_IDNb.Text;
            int state;
            int mtype;

            switch (com_MemState.Text)
            {
                case "可用":
                    state = 1;
                    break;
                case "不可用":
                    state = 0;
                    break;
                default:
                    MessageBox.Show("请正确选择状态类型");
                    return;
            }
            switch (com_MemType.Text)
            {
                case "白金卡":
                    mtype = 0;
                    break;
                case "金卡":
                    mtype = 1;
                    break;
                case "银卡":
                    mtype = 2;
                    break;
                default:
                    MessageBox.Show("请正确选择会员类型");
                    return;

            }
            tb_member member = new tb_member();
            member.memberID = id;
            member.memberName = mename;
            member.phone = rephone;
            member.address = address;
            member.IDNumber = idnumber;
            member.type = mtype;
            member.sumGrade = 0;
            member.isUse = state;
            conn.tb_member.Add(member);
            conn.SaveChanges();
            MessageBox.Show("会员注册成功！");
        }
    }
}
